package com.lsh.action.system;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lsh.action.AbstractManagerAction;
import com.lsh.model.TsysRight;
import com.lsh.service.TsysRightService;

@ParentPackage("test-default")
@Namespace(value = "/system")
@Results( { @Result(name = "success", location = "/system/sysRightList.jsp"),
		@Result(name = "sysRightAdd", location = "/system/sysRightAdd.jsp"),
		@Result(name = "sysRightEdit", location = "/system/sysRightEdit.jsp"),
			@Result(name = "sysRightView", location = "/system/sysRightView.jsp") })
public class TsysRightAction extends AbstractManagerAction {
	
	private static final long serialVersionUID = 1L;
	private final static Log LOG = LogFactory.getLog(TsysRightAction.class);

	@Autowired
	private TsysRightService tsysRightService;
	private TsysRight tsysRight;
	private String id;
	private String ids;//批量操作的ID
	private String flag;//edit,save
	
	public TsysRightAction() {
		backUrlHead="/system/sysRightList.do";
	}
	@Action("sysRightList")
	public String list() {
		try {
			Map<String,Object> map = new HashMap<String,Object>();
//			page = TsysRightService.getPage(map, page);
//			LOG.info((page.getPageNo()));
//			page.setItems((List<TsysRight>)page.getItems());
			datas = tsysRightService.getAll();
			
			tsysRight= tsysRightService.getTree("ROOT");
			return SUCCESS;  
		} catch (Exception e) {
			e.printStackTrace();
			message="显示列表出错了";
			LOG.error("列表出错了");
			return ERROR;
		}
	}

	@Action("sysRightAdd")
	public String add() {
		try {
			if(flag==null || "".equals(flag)){
				return "sysRightAdd";
			}
			tsysRightService.insert(tsysRight);
			message= "添加信息成功";
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			message="添加信息出错了";
			LOG.error("添加出错了");
			return ERROR;
		}
	}

	@Action("sysRightDelete")
	public String delete() {
		try {
			tsysRight = tsysRightService.getById(String.valueOf(id));
			if(tsysRight!=null){
				tsysRightService.delete(tsysRight);
				message= "删除成功";
			}else{
				message="没有该记录";
			}
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			message="删除信息出错了";
			LOG.error("删除出错了");
			return ERROR;
		}
	}
	
	@Action("sysRightDeleteAll")
	public String deleteAll() {
		try {
			message= "批量删除成功";
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			message="批量删除出错了";
			LOG.error("批量删除出错了");
			return ERROR;
		}
	}
	
	@Action("sysRightEdit")
	public String edit() {
		try {
			if(flag==null || "".equals(flag)){
				tsysRight=tsysRightService.getById(String.valueOf(id));
				return "sysRightEdit";
			}
			tsysRightService.update(tsysRight);
			message= "修改信息成功";  
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			message= "修改信息出错"; 
			LOG.error("更新出错了"); 
			return ERROR;
		}
	}

	@Action("sysRightView")
	public String view() {
		try {
			if(id!=null && !id.equals("")){
				tsysRight=tsysRightService.getById(String.valueOf(id));
				return "sysRightView";
			}
			return "sysRightView";
		} catch (Exception e) {
			e.printStackTrace();
			message= "查看信息出错"; 
			LOG.error("查看出错了"); 
			return ERROR;
		}
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public TsysRight getTsysRight() {
		return tsysRight;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public void setTsysRight(TsysRight TsysRight) {
		this.tsysRight = TsysRight;
	}
}
